package com.ylr.send.framework.resolver;

import com.ylr.send.common.dto.SendSmsDto;
import com.ylr.base.common.exception.YlrException;
import com.ylr.base.common.tool.Result;
import com.ylr.send.common.vo.BatchSendSmsVo;
import com.ylr.send.common.vo.SendSmsVo;
import com.ylr.send.framework.feign.client.YsSmsClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-09-05 11:17:16
 * className: YsSmsResolver
 * version: 1.0
 * description:
 */
@Component
public class YsSmsResolver {

    private static final Logger log = LoggerFactory.getLogger(YsSmsResolver.class);

    @Value("${spring.application.simple-name:ylr}")
    private String applicationSimpleName;

    private final YsSmsClient ysSmsClient;

    public YsSmsResolver(YsSmsClient ysSmsClient) {
        this.ysSmsClient = ysSmsClient;
    }

    /**
     * 发送短信接口
     * @param sendSmsVo 参数对象
     * @return 发送结果
     */
    public SendSmsDto sendSms(SendSmsVo sendSmsVo) {
        Result<SendSmsDto> result = ysSmsClient.sendSms(sendSmsVo);
        if (result.isFail()) {
            log.warn("{}模块 => 发送短信失败。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * 批量发送短信接口
     * @param batchSendSmsVo 参数对象
     * @return 发送结果
     */
    public SendSmsDto batchSendSms(@RequestBody BatchSendSmsVo batchSendSmsVo) {
        Result<SendSmsDto> result = ysSmsClient.batchSendSms(batchSendSmsVo);
        if (result.isFail()) {
            log.warn("{}模块 => 批量发送短信失败。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }
}
